using System;

namespace Mite
{
	/// <summary>
	/// Utiltiy class for Dsl naming convention.
	/// </summary>
	public static class DslFileNameStandards
	{
		/// <summary>
		/// Parses a name and returns the version number implied by the naming standard if
		/// available.
		/// </summary>
		/// <example>
		/// migration_001 = 1
		/// migration_201 = 201
		/// </example>
		/// <param name="name"></param>
		/// <returns></returns>
		public static int GetVersion(this string name)
		{
			int lastUnderscoreIndex = name.LastIndexOf('_');
			if (lastUnderscoreIndex == -1)
			{
				throw new ArgumentException(
					"String does not match standards.  The standards require that a _ followed by a string of numbers be provided e.g. migration_001.boo",
					"name");
			}
			int version;
			if (!int.TryParse(name.Substring(lastUnderscoreIndex + 1), out version))
			{
				throw new ArgumentException(
					"String does not match standards.  The standards require that a _ followed by a string of numbers be provided e.g. migration_001.boo",
					"name");
			}
			return version;
		}
	}
}